<template>
  <view>
    <!-- 用户信息 -->
    <view class="user_info">
      <view class="user_icon">
        <img :src="imgDetail.user.avatar" alt="" mode="widthFix"
      /></view>
      <view class="user_desc">
        <view class="user_name">{{ imgDetail.user.name }}</view>
        <view class="user_time">{{ imgDetail.cnTime }}</view>
      </view>
    </view>
    <!-- 高清大图 -->
    <view class="high_img">
      <swiper-action @swiperAction="handleSwiperAction">
        <img mode="widthFix" :src="imgDetail.thumb" alt=""
      /></swiper-action>
    </view>
    <!-- 点赞 -->
    <view class="user_rank">
      <view class="rank">
        <text class="iconfont icondianzan">{{ imgDetail.rank }}</text>
      </view>
      <view class="user_collect">
        <text class="iconfont iconshoucan">收藏</text>
      </view>
    </view>
    <!-- 专辑 -->
    <view class="album_wrap" v-if="album.length">
      <view class="album_title">相关</view>
      <view class="album_list">
        <view class="album_item" v-for="item in album" :key="item.id">
          <view class="album_cover">
            <img :src="item.cover" alt="" mode="aspectFill" />
          </view>
          <view class="album_info">
            <view class="album_info_text">专辑</view>
            <view class="album_name">{{ item.name }}</view>
            <view class="iconfont iconiconfontjiantou4"></view>
          </view>
        </view>
      </view>
    </view>
    <!-- 最热评论 -->
    <view class="comment hot" v-if="hot.length">
      <view class="comment_title">
        <text class="iconfont iconhot1"></text>
        <text class="comment_text">最热评论</text>
      </view>
      <view class="comment_list">
        <view class="comment_item" v-for="item in hot" :key="item.id">
          <view class="comment_user">
            <!-- 用户头像 -->
            <view class="user_icon">
              <img :src="item.user.avatar" alt="" mode="widthFix" />
              <!-- 用户名称 --></view
            >
            <view class="user_name">
              <view class="user_nickname">{{ item.user.name }}</view>
              <view class="user_time">{{ item.cnTime }}</view>
            </view>
            <!-- 用户徽章 -->
            <view class="user_badge">
              <img
                :src="item2.icon"
                alt=""
                v-for="item2 in item.user.title"
                :key="item2.icon"
              />
            </view>
          </view>
          <!-- 评论 -->
          <view class="comment_desc">
            <view class="comment_content">{{ item.content }}</view>
            <view class="comment_like">
              <view class="iconfont icondianzan">{{ item.size }}</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 最新评论 -->
    <view class="comment new" v-if="comment.length">
      <view class="comment_title">
        <text class="iconfont iconpinglun"></text>
        <text class="comment_text">最新评论</text>
      </view>
      <view class="comment_list">
        <view class="comment_item" v-for="item in comment" :key="item.id">
          <view class="comment_user">
            <!-- 用户头像 -->
            <view class="user_icon">
              <img :src="item.user.avatar" alt="" mode="widthFix" />
              <!-- 用户名称 --></view
            >
            <view class="user_name">
              <view class="user_nickname">{{ item.user.name }}</view>
              <view class="user_time">{{ item.cnTime }}</view>
            </view>
            <!-- 用户徽章 -->
            <view class="user_badge">
              <img
                :src="item2.icon"
                alt=""
                v-for="item2 in item.user.title"
                :key="item2.icon"
              />
            </view>
          </view>
          <!-- 评论 -->
          <view class="comment_desc">
            <view class="comment_content">{{ item.content }}</view>
            <view class="comment_like">
              <view class="iconfont icondianzan">{{ item.size }}</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 下载图片 -->
    <!-- <view class="download">
      <view class="download_btn" @click="handleDown">下载图片</view>
    </view> -->
  </view>
</template>

<script>
import moment from "moment";
import swiperAction from "@/components/swiperAction";
moment.locale("zh-cn");
export default {
  components: {
    swiperAction
  },
  data() {
    return {
      imgDetail: {},
      album: [],
      // 最热评论
      hot: [],
      // 最新评论
      comment: [],
      // 图片的索引
      imgIndex: 0
    };
  },
  onLoad() {
    const { imgIndex } = getApp().globalData;
    this.imgIndex = imgIndex;
    this.getData();
  },
  methods: {
    getData() {
      const { imgList } = getApp().globalData;
      this.imgDetail = imgList[this.imgIndex];
      // this.imgDetail.newThumb =
      //   this.imgDetail.thumb + this.imgDetail.rule.replace("$<Height>", 360);
      this.imgDetail.cnTime = moment(this.imgDetail.atime * 1000).fromNow();
      this.getComments(this.imgDetail.id);
    },
    async getComments(id) {
      const res = await this.request(
        `/image/v2/wallpaper/wallpaper/${id}/comment`
      );

      if (res.code === 0) {
        this.album = res.res.album;
        res.res.hot.forEach(element => {
          element.cnTime = moment(element.atime * 1000).fromNow();
        });
        res.res.comment.forEach(element => {
          element.cnTime = moment(element.atime * 1000).fromNow();
        });
        this.hot = res.res.hot;
        this.comment = res.res.comment;
      }
    },

    handleSwiperAction(e) {
      const { imgList } = getApp().globalData;
      //需要判断数据下标越界
      if (e.direction === "left" && this.imgIndex < imgList.length - 1) {
        this.imgIndex++;
        this.getData();
      } else if (e.direction === "right" && this.imgIndex > 0) {
        this.imgIndex--;
        this.getData();
      } else {
        uni.showToast({
          title: "没有图片了",
          icon: "none"
        });
      }
    },
    async handleDown() {
      /**
       * 远程文件下载到小程序 tempFilePath
       */
      await uni.showLoading({
        title: "下载中"
      });
      const imgurl = this.imgDetail.img;
      const { tempFilePath } = (
        await uni.downloadFile({
          url: imgurl
        })
      )[1];

      //console.log(tempFilePath);
      /**
       * 将小程序临时文件下载到本地
       */
      await uni.saveImageToPhotosAlbum({
        filePath: tempFilePath
      });

      uni.hideLoading();
      await uni.showToast({
        title: "下载成功"
      });
    }
  }
};
</script>

<style lang="scss">
.user_info {
  display: flex;
  padding: 20rpx;
  .user_icon {
    padding: 0 20rpx;

    img {
      width: 88rpx;
      border-radius: 50%;
    }
  }
  .user_desc {
    .user_name {
      color: #000;
      font-weight: 600;
    }

    .user_time {
      padding: 10rpx 0;
      color: #ccc;
      font-size: 24rpx;
    }
  }
}

.user_rank {
  display: flex;
  height: 80rpx;
  border-bottom: 5px solid #eee;
  .rank {
    display: flex;
    flex: 1;
    justify-content: center;
    align-items: center;
    text.iconfont.icondianzan {
    }
  }

  .user_collect {
    display: flex;
    flex: 1;
    justify-content: center;
    align-items: center;
    text.iconfont.iconshoucan {
    }
  }
}
.album_wrap {
  padding: 20rpx;
  .album_title {
    padding: 10rpx o;
  }

  .album_list {
    .album_item {
      display: flex;
      padding: 10rpx 0;
      border-bottom: 10px solid #eee;
      .album_cover {
        flex: 1;
        img {
          width: 180rpx;
          height: 180rpx;
        }
      }

      .album_info {
        flex: 3;
        padding-left: 20rpx;
        position: relative;
        .album_info_text {
          color: white;
          width: 100rpx;
          height: 50rpx;
          background-color: $color;
          display: flex;
          justify-content: center;
          align-items: center;
        }

        .album_name {
          padding: 10rpx 0;
          color: #888;
        }
        .iconfont {
          font-size: 40rpx;
          position: absolute;
          top: 50%;
          right: 10%;
          transform: translateY(-50%);
          color: #000;
          //搜索CSS3 变换或者转换
        }
      }
    }
  }
}
.high_img {
  img {
    border-bottom: 1px solid #eee;
  }
}
.comment {
  .comment_title {
    padding: 15rpx;
    text.iconfont.iconhot1 {
      color: red;
      font-size: 40rpx;
    }

    text.comment_text {
      font-weight: 600;
      font-size: 28rpx;
      color: #000;
      margin-left: 10rpx;
    }
  }

  .comment_list {
    .comment_item {
      border-bottom: 15px solid #eee;

      //用户信息
      .comment_user {
        padding: 20rpx 0;
        display: flex;
        .user_icon {
          width: 15%;
          display: flex;
          justify-content: center;
          align-items: center;
          img {
            width: 90%;
          }
        }

        .user_name {
          flex: 1;
          .user_nickname {
            color: #777;
          }

          .user_time {
            color: #ccc;
            font-size: 24rpx;
            padding: 5rpx;
          }
        }

        .user_badge {
          img {
            width: 40rpx;
            height: 40rpx;
            display: inline-block;
          }
        }
      }

      //评论
      .comment_desc {
        display: flex;
        padding: 30rpx 0;
        .comment_content {
          flex: 1;
          padding-left: 15%;
          color: #000;
        }

        .comment_like {
          text-align: right;
          .iconfont.icondianzan {
          }
        }
      }
    }
  }
}
.new {
  .iconpinglun {
    color: aqua !important;
  }
}
.download {
  height: 120rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  .download_btn {
    width: 90%;
    height: 80%;
    background-color: $color;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 50rpx;
  }
}
</style>
